#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define x first
#define y second
#define pb push_back
using namespace std;
const int N = 1e5 + 5;
int n,m,a[N],s[N],p[N],g[20][N],c[N],k=1;
bool f[20][N];
bool work(int x){
    for(int i=1;i<=k;++i){
        for(int j=1;j<=(1<<(k-i));++j){
            int l = g[i-1][j*2-1],r = g[i-1][j*2];
            int u = 0;
            if(f[i][j]) u = max(l,r);
            else u = min(l,r);
            if(p[u]>=i) g[i][j] = u;
            else g[i][j] = l + r - u;
            if(l==x && g[i][j]!=x) return 0;
            if(r==x && g[i][j]!=x) return 0;
        }
    }
    return 1;
}
int tmp[4];
ll calc(int x){
    ll ans = 0;
    k = 0;
    while((1<<k)<x) k++;
    int tot = 1<<k;
    for(int i=1;i<=tot;++i){
        for(int j=1;j<=x;++j) g[0][j] = j,p[j] = s[j];
        for(int j=x+1;j<=tot;++j){
            g[0][j] = j;
            if(j==i) p[j] = k + 1;
            else p[j] = 0;
        }
        ans += work(i) * i;
    }
    return ans;
}
void solve(){
    for(int i=0;i<4;++i) cin>>tmp[i];
    for(int i=1;i<=n;++i) s[i] = a[i] ^ tmp[i%4];
    ll ans = 0;
    for(int i=1;i<=m;++i) ans ^= calc(c[i]) * i;
    cout<<ans<<'\n';
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n;++i) cin>>a[i];
    for(int i=1;i<=m;++i) cin>>c[i];
    while((1<<k)<n) k++;
    for(int i=1;i<=k;++i){
        for(int j=1;j<=(1<<(k-i));++j){
            char cc;cin>>cc;
            f[i][j] = cc - '0';
        }
    }
    int T;cin>>T;
    while(T--) solve();
    return 0;
}
